{% extends "base.html" %}

{% block content %}
<div class="container">
    <h2 class="mb-4">安全事故管理</h2>
    
    <div class="card mb-4">
        <div class="card-body">
            <form method="GET" class="row g-3">
                <div class="col-md-2">
                    <input type="text" class="form-control" name="product_name" placeholder="产品名称" value="{{ product_name }}">
                </div>
                <div class="col-md-2">
                    <input type="text" class="form-control" name="batch_number" placeholder="批次号" value="{{ batch_number }}">
                </div>
                <div class="col-md-2">
                    <select class="form-select" name="incident_type">
                        <option value="">事故类型</option>
                        <option value="contamination" {% if incident_type == 'contamination' %}selected{% endif %}>污染</option>
                        <option value="temperature" {% if incident_type == 'temperature' %}selected{% endif %}>温度异常</option>
                        <option value="damage" {% if incident_type == 'damage' %}selected{% endif %}>产品损坏</option>
                    </select>
                </div>
                <div class="col-md-2">
                    <select class="form-select" name="severity">
                        <option value="">严重程度</option>
                        <option value="low" {% if severity == 'low' %}selected{% endif %}>轻微</option>
                        <option value="medium" {% if severity == 'medium' %}selected{% endif %}>中等</option>
                        <option value="high" {% if severity == 'high' %}selected{% endif %}>严重</option>
                        <option value="critical" {% if severity == 'critical' %}selected{% endif %}>危急</option>
                    </select>
                </div>
                <div class="col-md-2">
                    <select class="form-select" name="status">
                        <option value="">处理状态</option>
                        <option value="open" {% if status == 'open' %}selected{% endif %}>待处理</option>
                        <option value="investigating" {% if status == 'investigating' %}selected{% endif %}>调查中</option>
                        <option value="resolved" {% if status == 'resolved' %}selected{% endif %}>已解决</option>
                        <option value="closed" {% if status == 'closed' %}selected{% endif %}>已关闭</option>
                    </select>
                </div>
                <div class="col-md-2">
                    <button type="submit" class="btn btn-primary w-100">搜索</button>
                </div>
            </form>
        </div>
    </div>

    <div class="mb-3">
        <a href="{{ url_for('safety.incident_add') }}" class="btn btn-success">
            <i class="fas fa-plus"></i> 新增事故
        </a>
    </div>

    <div class="table-responsive">
        <table class="table table-striped table-hover">
            <thead>
                <tr>
                    <th>事故编号</th>
                    <th>产品名称</th>
                    <th>批次号</th>
                    <th>事故类型</th>
                    <th>严重程度</th>
                    <th>发生时间</th>
                    <th>状态</th>
                    <th>操作</th>
                </tr>
            </thead>
            <tbody>
                {% for incident in incidents %}
                <tr>
                    <td>{{ incident.incident_no }}</td>
                    <td>{{ incident.product_name }}</td>
                    <td>{{ incident.batch_number }}</td>
                    <td>
                        {% if incident.incident_type == 'contamination' %}
                            污染
                        {% elif incident.incident_type == 'temperature' %}
                            温度异常
                        {% else %}
                            产品损坏
                        {% endif %}
                    </td>
                    <td>
                        {% if incident.severity == 'low' %}
                            <span class="badge bg-info">轻微</span>
                        {% elif incident.severity == 'medium' %}
                            <span class="badge bg-warning">中等</span>
                        {% elif incident.severity == 'high' %}
                            <span class="badge bg-danger">严重</span>
                        {% else %}
                            <span class="badge bg-dark">危急</span>
                        {% endif %}
                    </td>
                    <td>{{ incident.occurrence_date.strftime('%Y-%m-%d %H:%M') }}</td>
                    <td>
                        {% if incident.status == 'open' %}
                            <span class="badge bg-danger">待处理</span>
                        {% elif incident.status == 'investigating' %}
                            <span class="badge bg-warning">调查中</span>
                        {% elif incident.status == 'resolved' %}
                            <span class="badge bg-success">已解决</span>
                        {% else %}
                            <span class="badge bg-secondary">已关闭</span>
                        {% endif %}
                    </td>
                    <td>
                        <a href="{{ url_for('safety.incident_edit', id=incident.id) }}" class="btn btn-sm btn-primary">
                            <i class="fas fa-edit"></i> 编辑
                        </a>
                    </td>
                </tr>
                {% endfor %}
            </tbody>
        </table>
    </div>

    {% if pagination.pages > 1 %}
    <nav aria-label="Page navigation">
        <ul class="pagination justify-content-center">
            {% for page in pagination.iter_pages() %}
                {% if page %}
                    <li class="page-item {% if page == pagination.page %}active{% endif %}">
                        <a class="page-link" href="{{ url_for('safety.incident_list', page=page, product_name=product_name, batch_number=batch_number, incident_type=incident_type, severity=severity, status=status) }}">{{ page }}</a>
                    </li>
                {% else %}
                    <li class="page-item disabled"><span class="page-link">...</span></li>
                {% endif %}
            {% endfor %}
        </ul>
    </nav>
    {% endif %}
</div>
{% endblock %} 